 /*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-06     DRZZZ       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <stdio.h>
#include "board.h"
#include "water.h"
#include "connect_8266.h"
#include "SR04.h"

#define LOG_TAG        "Chack"
#define LOG_LVL        LOG_LVL_INFO
#include <ulog.h>

#define THREAD_PRIORITY         2
#define THREAD_TIMESLICE        100

static int32_t  cnt1 = 0,
                cnt2 = 0,
                cnt3 = 0,
                cnt4 = 0,
                cntsecond = 0,
                pr_cnt = 0;




ALIGN(RT_ALIGN_SIZE)
static char water_stack[2048];
static struct rt_thread water_thread;


static void cap1_enter()
{
    cnt1++;
}
static void cap2_enter()
{
    cnt2++;
}
static void cap3_enter()
{
    cnt3++;
}
static void cap4_enter()
{
    cnt4++;
}
//static void cap_second_enter()
//{
//    cntsecond++;
//}

static char data_show_flag = 0;
static char wave_show_flag = 0;
void Oil_data(void) {
    if(data_show_flag == 0)
        data_show_flag = 1;
}
void Oil_Wave(void) {
    if(wave_show_flag == 0)
        wave_show_flag = 1;
}

MSH_CMD_EXPORT(Oil_data, Oil content detection);
MSH_CMD_EXPORT(Oil_Wave, Oil Waveform display);
static void water_entry(void *param)
{
    while(1)
    {
        pr_cnt++;
        if(pr_cnt == 60 && data_show_flag == 1) {
            pr_cnt = 0;
            rt_kprintf("-------------- Filtration --------------\n");
            rt_kprintf("1:%ld -- 2:%ld -- 3:%ld -- 4:%ld -- Second:%ld\n", cnt1, cnt2, cnt3, cnt4, cntsecond);
        }
        if(pr_cnt == 60 && wave_show_flag == 1) {
            pr_cnt = 0;
            rt_kprintf("oil:%ld,%ld,%ld,%ld\n", cnt1, cnt2, cnt3, cnt4);
        }
        cnt1 = 0;
        cnt2 = 0;
        cnt3 = 0;
        cnt4 = 0;
        cntsecond = 0;
        rt_thread_mdelay(50);
    }
}

static int Water_thread(void)
{
    /* 按键0引脚为输入模式 */
    rt_pin_mode(     CAP_TEST_1, PIN_MODE_INPUT );
    rt_pin_mode(     CAP_TEST_2, PIN_MODE_INPUT );
    rt_pin_mode(     CAP_TEST_3, PIN_MODE_INPUT );
    rt_pin_mode(     CAP_TEST_4, PIN_MODE_INPUT );
//    rt_pin_mode(CAP_TEST_SECOND, PIN_MODE_INPUT );
    /* 绑定中断，下降沿模式，回调函数名为beep_on */
    rt_pin_attach_irq(     CAP_TEST_1, PIN_IRQ_MODE_RISING,       cap1_enter, RT_NULL);
    rt_pin_attach_irq(     CAP_TEST_2, PIN_IRQ_MODE_RISING,       cap2_enter, RT_NULL);
    rt_pin_attach_irq(     CAP_TEST_3, PIN_IRQ_MODE_RISING,       cap3_enter, RT_NULL);
    rt_pin_attach_irq(     CAP_TEST_4, PIN_IRQ_MODE_RISING,       cap4_enter, RT_NULL);
//    rt_pin_attach_irq(CAP_TEST_SECOND, PIN_IRQ_MODE_RISING, cap_second_enter, RT_NULL);
    /* 使能中断 */
    rt_pin_irq_enable(     CAP_TEST_1, PIN_IRQ_ENABLE);
    rt_pin_irq_enable(     CAP_TEST_2, PIN_IRQ_ENABLE);
    rt_pin_irq_enable(     CAP_TEST_3, PIN_IRQ_ENABLE);
    rt_pin_irq_enable(     CAP_TEST_4, PIN_IRQ_ENABLE);
//    rt_pin_irq_enable(CAP_TEST_SECOND, PIN_IRQ_ENABLE);

    rt_thread_init(&water_thread,
                   "water_thread",
                   water_entry,
                   RT_NULL,
                   &water_stack[0],
                   sizeof(water_stack),
                   THREAD_PRIORITY, THREAD_TIMESLICE);
    rt_thread_startup(&water_thread);
    LOG_I("--Water_Chack init over!");
    return RT_EOK;
}
/* 导出到 msh 命令列表中 */

INIT_DEVICE_EXPORT(Water_thread);
